package pl.com.hme.kody.pocztowe.data;

import java.text.Collator;
import java.util.Locale;

public class PointOfInterest implements Comparable<PointOfInterest> {
	private String mCode = null; // kod pocztowy
	private String mLocality = null; // miejscowosc
	private String mStreetNumbers = null; // numery ulic
	private String mProvince = null; // wojewodztwo
	private String mDistrict = null; // powiat
	private String mBorough = null; // gmina
	private String mStreet = null; // ulica

	public boolean isValid() {
		if (mCode != null && mLocality != null && mStreetNumbers != null && mProvince != null && mDistrict != null
				&& mBorough != null && mStreet != null)
			return true;

		return false;
	}

	public String getCode() {
		return mCode;
	}

	public void setCode(String code) {
		mCode = code;
	}

	public String getLocality() {
		return mLocality;
	}

	public void setLocality(String locality) {
		mLocality = locality;
	}

	public String getStreetNumbers() {
		return mStreetNumbers;
	}

	public void setStreetNumbers(String streetNumbers) {
		mStreetNumbers = streetNumbers;
	}

	public String getProvince() {
		return mProvince;
	}

	public void setProvince(String province) {
		mProvince = province;
	}

	public String getDistrict() {
		return mDistrict;
	}

	public void setDistrict(String district) {
		mDistrict = district;
	}

	public String getBorough() {
		return mBorough;
	}

	public void setBorough(String borough) {
		mBorough = borough;
	}

	public String getStreet() {
		return mStreet;
	}

	public void setStreet(String street) {
		mStreet = street;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((mBorough == null) ? 0 : mBorough.hashCode());
		result = prime * result + ((mCode == null) ? 0 : mCode.hashCode());
		result = prime * result + ((mDistrict == null) ? 0 : mDistrict.hashCode());
		result = prime * result + ((mLocality == null) ? 0 : mLocality.hashCode());
		result = prime * result + ((mProvince == null) ? 0 : mProvince.hashCode());
		result = prime * result + ((mStreetNumbers == null) ? 0 : mStreetNumbers.hashCode());
		result = prime * result + ((mStreet == null) ? 0 : mStreet.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PointOfInterest other = (PointOfInterest) obj;
		if (mBorough == null) {
			if (other.mBorough != null)
				return false;
		} else if (!mBorough.equals(other.mBorough))
			return false;
		if (mCode == null) {
			if (other.mCode != null)
				return false;
		} else if (!mCode.equals(other.mCode))
			return false;
		if (mDistrict == null) {
			if (other.mDistrict != null)
				return false;
		} else if (!mDistrict.equals(other.mDistrict))
			return false;
		if (mLocality == null) {
			if (other.mLocality != null)
				return false;
		} else if (!mLocality.equals(other.mLocality))
			return false;
		if (mProvince == null) {
			if (other.mProvince != null)
				return false;
		} else if (!mProvince.equals(other.mProvince))
			return false;
		if (mStreetNumbers == null) {
			if (other.mStreetNumbers != null)
				return false;
		} else if (!mStreetNumbers.equals(other.mStreetNumbers))
			return false;
		if (mStreet == null) {
			if (other.mStreet != null)
				return false;
		} else if (!mStreet.equals(other.mStreet))
			return false;
		return true;
	}

	private int compareTwoLocalStrings(String thisString, String otherString) {
		Collator localCollator = Collator.getInstance(new Locale("pl", "PL"));

		return localCollator.compare(thisString, otherString);
	}

	@Override
	public int compareTo(PointOfInterest poi) {
		// prov , disc , borough,

		int compare = compareTwoLocalStrings(mProvince, poi.getProvince());

		if (compare == 0) {
			compare = compareTwoLocalStrings(mDistrict, poi.getDistrict());

			if (compare == 0) {
				compare = compareTwoLocalStrings(mBorough, poi.getBorough());

				if (compare == 0) {
					compare = compareTwoLocalStrings(mLocality, poi.getLocality());

					if (compare == 0) {
						compare = compareTwoLocalStrings(mStreet, poi.getStreet());
						if (compare == 0) {
							String[] thisCodes = mCode.split("-", 2);
							String[] otherCodes = poi.getCode().split("-", 2);

							Integer thisCode = new Integer(thisCodes[0]);
							Integer otherCode = new Integer(otherCodes[0]);

							compare = thisCode.compareTo(otherCode);

							if (compare == 0) {
								thisCode = new Integer(thisCodes[1]);
								otherCode = new Integer(otherCodes[1]);
								compare = thisCode.compareTo(otherCode);
							}
						}
					}
				}
			}
		}
		return compare;
	}
}
